//
//  SerialViewController.m
//  WirelessVehicle
//
//  Created by JianWei Chen on 16/6/12.
//  Copyright © 2016年 JianWei Chen. All rights reserved.
//

#import "WebViewController.h"
#import "IMYWebView.h"
#import "NavigatObject.h"

@interface WebViewController ()<UIWebViewDelegate,IMYWebViewDelegate>

@property (strong, nonatomic) IMYWebView *webview;

@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setLeftButtonImage:nil];
    [self setLeftButtonImage:[UIImage imageNamed:@"tittle_icon_back"]];
    [self setNavigationBarTitle:self.title];
    [self setWebData];
    
    // Do any additional setup after loading the view.
}

- (void)setWebData
{
    [_webview removeFromSuperview];
    _webview = [[IMYWebView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    [self.view addSubview:_webview];
    _webview.delegate=self;

    NSURLRequest *request = [NSURLRequest requestWithURL:self.url];
    
    
    [_webview loadRequest:request];
}





-(void)webView:(IMYWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"didFailLoadWithError=====%@",error.description);
    
}
-(void)webViewDidFinishLoad:(IMYWebView *)webView
{
    
    NSLog(@"webViewDidFinishLoad=====%@",webView);
}
-(BOOL)webView:(IMYWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"===========shouldStartLoadWithRequest========%@====%ld",request.URL.absoluteString,(long)navigationType);
    
    NavigatObject *naviga=[[NavigatObject alloc]init];
    [naviga stringToHtmlString:request.URL.absoluteString];
    
    if ([naviga.Paramsdic[@"action"] isEqualToString:@"OPEN_WEBVIEW"]) {

        [self OPEN_WEBVIEW:naviga.Paramsdic];
        
        return NO;
    }else if ([request.URL.absoluteString rangeOfString:@"UPDATE_ADVICE"].location != NSNotFound){
        [self UPDATE_ADVICE:naviga.Paramsdic];
    }
    
    return YES;
}

- (void)OPEN_WEBVIEW:(NSDictionary *)paramsDic
{
    NSArray *nameArray = [paramsDic[@"url"] componentsSeparatedByString:@"."];
    WebViewController *vc = [[WebViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:nameArray[0] ofType:@".html"]];
    NSString *str = [url.absoluteString componentsSeparatedByString:[NSString stringWithFormat:@"%@%@",nameArray[0],@".html"]][0];
    url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",str,paramsDic[@"url"]]];
    vc.url = url;
    vc.title = paramsDic[@"title"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)UPDATE_ADVICE:(NSDictionary *)paramsDic
{
    [self getAdList:paramsDic[@"phone"] andConten:paramsDic[@"content"]];
}

- (void)getAdList:(NSString *)phone andConten:(NSString *)content
{
//    UserFeedback * getApi = [[UserFeedback alloc] initWithPhone:phone andConten:content];
//    
//    [getApi startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
//        if ([getApi analyseResponseObject]) {
//            [self.navigationController popViewControllerAnimated:YES];
//        }
//    } failure:^(YTKBaseRequest *request) {
//        [SVProgressHUD showInfoWithStatus:@"网络异常"];
//    }];
}


-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
    NSLog(@"main dealloc  `");
}




@end
